﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;

using Telerik.Cms.Engine;
using Telerik.Lists;

namespace Telerik.Sitefinity.Services.Migration
{
	public partial class MigrationExportService : IMigrationExportService
	{
		public Message GetLists()
		{
			List<MigrationList> output = new List<MigrationList>();

			IDictionary<string, ListProvider> listProviders = ListManager.Providers;

			foreach (KeyValuePair<string, ListProvider> listProvider in listProviders)
			{
				string providerName = listProvider.Key;
				ListManager listManager = new ListManager(providerName);

				IList lists = listManager.GetLists();
				foreach (INamedList list in lists)
				{
					MigrationList ml = new MigrationList();
					ml.Created = list.Created;
					ml.Id = list.ID;
					ml.Name = list.Name;
					ml.Owner = list.Owner;
					ml.ProviderName = providerName;

					foreach (IListItem listItem in list.Items)
					{
						MigrationListItem mli = new MigrationListItem();
						mli.Content = listItem.Content;

#if VERSION_BELOW_SP3
						mli.HasDynamicLinks = false;
#else
						mli.HasDynamicLinks = listItem.HasDynamicLinks;
#endif

						mli.Headline = listItem.Headline;
						mli.Id = listItem.ID;
						mli.Ordinal = listItem.Ordinal;

						ml.Items.Add(mli);
					}

					output.Add(ml);
				}
			}

			MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
			return Message.CreateMessage(ver, "GetListsResponse", output);
		}
	}
}
